package com.pension.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pension.entity.User;
import com.pension.service.ServiceService;
import com.pension.service.UserService;
import com.pension.utils.LatitudeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ServiceService serviceService;



    //开发用户登录
    @RequestMapping(value = "/login.do")
    public String login(String username, String password, HttpSession session, Model model) {
        User user = userService.login(username, password);
        Integer identity = userService.authoritymanagement(username, password);
        //判断  user 为空  用户户名密码错误      //不为空  登录成功
        if (user != null && identity == 1) {
            session.setAttribute("user", user);
            session.setAttribute("s_lat",user.getLat());
            session.setAttribute("s_lng",user.getLng());
            session.setAttribute("realname", user.getRealname());
            session.setAttribute("place", user.getAddress_place());
            session.setAttribute("longitude", user.getLng());
            session.setAttribute("latitude", user.getLat());
            return "index";//查询员工的所有
        } else if (user != null && identity == 2) {  //跳转到后台管理界面
            session.setAttribute("user", user);
            return "index2";//查询员工的所有
        } else if (user==null){
            model.addAttribute("msg", "注册失败");
            return "register";
        }else {
            return "register";
        }
    }
    //开发用户注册
    @RequestMapping("/regist.do")
    public String regist(User user, HttpSession session){
       user.setAddress_city("北京市");
       user.setAddress_place(user.getAddress_city()+user.getAddress_area()+user.getAddress_street());
       user.setP_identity(1);
        Map<String, String> json = LatitudeUtils.getGeocoderLatitude(user.getAddress_place());
        user.setLat(json.get("lat"));
        user.setLng(json.get("lng"));
       if ( userService.save(user)){
           return "login";
       }else {
           return "register";
       }
    }

    @RequestMapping("/toregist.do")
    public ModelAndView toregist(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mView = new ModelAndView("register");
        return mView;
    }


    /**
     * 依据id返回对应的用户信息
     * @param id
     * @return
     */
    @RequestMapping(value="/queryStudentByJSZH.do",method= RequestMethod.GET)
    public ModelAndView queryStudentByJSZH(@RequestParam String id){
        ModelAndView mView=new ModelAndView("user");
        User user=userService.findUserByIdWithParam(id);
        mView.addObject("user", user);
        return mView;
    }

    /**
     * 更新用户信息
     * @param request
     * @param response
     * @param student
     * @return
     * @throws UnsupportedEncodingException
     */
    private String STUDENTLIST = "redirect:/student/list.do";
    @RequestMapping(value="/editStudent.do",method= RequestMethod.POST)
    public ModelAndView editStudent(HttpServletRequest request, HttpServletResponse response, User user) throws UnsupportedEncodingException {
        userService.updateUser(user);
        ModelAndView mView = new ModelAndView("right");
        return mView;
    }

    @RequestMapping(value = "/findAllUser?pn=1")
    public String jumpuserlist(){
        return "userList";
    }

  //查询所有用户
    @RequestMapping("/findAllUser")
    public String findAllUser(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model){
//        User user1 = new User();
        PageHelper.startPage(pn,6);
        List<User> user = userService.findAllUser();
        PageInfo page = new PageInfo(user,5);
        model.addAttribute("pageInfo",page);
        return "userList";
    }

    //改变权限--普通用户
    @RequestMapping("/updateauthority1")
    public ModelAndView updateauthority1(@RequestParam("id")int id){
        userService.authority(id);
        return new ModelAndView(userurl);
    }

    //改变权限--管理员
    @RequestMapping("/updateauthority2")
    public ModelAndView updateauthority2(@RequestParam("id")int id){
        userService.authority2(id);
        return new ModelAndView(userurl);
    }

    //删除用户
    String userurl="redirect:/findAllUser?pn=1";
    @RequestMapping("deleteByid")
    public ModelAndView  deleteByid(@RequestParam("id")int id){
        userService.deleteByid(id);
        return new ModelAndView(userurl);
    }

    //用户删除订单
    String userOrderUrl="redirect:/service/findByOldManRealName?oldpn=1";
    @RequestMapping("deleteOrderById")
    public ModelAndView  deleteOrderById(@RequestParam("id")int id){
        serviceService.serviceDeleteById(id);
        return new ModelAndView(userOrderUrl);
    }

}
